home *** CD-ROM | disk | FTP | other *** search
/ Gekikoh Dennoh Club 1 / Gekikoh Dennoh Club Vol. 1 (Japan).7z / Gekikoh Dennoh Club Vol. 1 (Japan) (Track 1).bin / kowin / archive / net / koprosrc.lzh / pack.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-05-30  |  2.2 KB  |  125 lines

  1. /*    Copyright 1992 H.Ogasawara(COR.)    */
  2.  
  3. #include    "proto.h"
  4.  
  5. /*
  6.     make a packet of data.
  7.     return value is packet size.
  8. */
  9. DataPack( block, pack, data, flag )
  10. unsigned char    *pack,
  11.         *data;
  12. {
  13.     unsigned short    i,
  14.             len= flag & 0xfff;
  15.     *pack++= len == 128 ? SOH : STX;
  16.     *pack++= block;
  17.     *pack++= ~block;
  18.     if( flag & 0x8000 ){    /* SUM */
  19.         unsigned short    sum;
  20.         for( sum= i= 0 ; i< len ; i++ )
  21.             sum+= (*pack++= *data++);
  22.         *pack++= sum;
  23.         return    len+ 4;
  24.     }else{            /* CRC */
  25.         unsigned short    crc= CRCTOP;
  26.         for( i= 0 ; i< len ; i++ )
  27.             crc= crc<<8^__crc[crc>>8^(*pack++= *data++)];
  28.         *pack++= crc>>8;
  29.         *pack++= crc&255;
  30.         return    len+ 5;
  31.     }
  32. }
  33.  
  34. /*
  35.     unpack and error check.
  36.     return value is data size.
  37.     if this routine returned -1, packet got error.
  38. */
  39. DataUnpack( len, pack, flag )
  40. unsigned short    len;
  41. unsigned char    *pack;
  42. {
  43.     unsigned short    i;
  44.     if( *pack != ((~pack[1]) & 0xff) )
  45.         return    -1;
  46.     pack+= SKIP;
  47.     if( flag ){
  48.         unsigned char    sum;
  49.         for( sum= i= 0 ; i< len ; i++ )
  50.             sum+= *pack++;
  51.         if( *pack != sum )
  52.             return    -1;
  53.         return    len;
  54.     }else{
  55.         unsigned short    crc= CRCTOP;
  56.         for( i= 0 ; i< len ; i++ )
  57.             crc= crc<<8^__crc[crc>>8^*pack++];
  58.         if( crc != (*pack << 8)+pack[1] )
  59.             return    -1;
  60.         return    len;
  61.     }
  62. }
  63.  
  64. /*
  65.     make ymodem first data.
  66. */
  67. SetFilePacket( buf, name, size, time )
  68. unsigned char    *buf,
  69.         *name;
  70. unsigned int    size,
  71.         time;
  72. {
  73.     unsigned char    *p,
  74.             *s,
  75.             pack[24];
  76.     for( p= buf ; *p++= *name++ ;);
  77.     s= pack+20;
  78.     *s= '\0';
  79.     do{
  80.         *--s= size%10+'0';
  81.     }while( size/= 10 );
  82.     for(; *s ; *p++= *s++ );
  83.     *p++= ' ';
  84.     do{
  85.         *--s= (time&7)+'0';
  86.     }while( size>>=3 );
  87.     while( *p++= *s++ );
  88.     for(; p < buf+128 ; *p++= '\0' );
  89. }
  90.  
  91. /*
  92.     get file name from first data.
  93. */
  94. GetFilePacket( data, name, size )
  95. unsigned char    *data,
  96.         *name;
  97. unsigned int    *size;
  98. {
  99.     unsigned int    i;
  100.     while( *name++= *data++ );
  101. #if 0
  102. ConsoleOpen();
  103. ConsolePrint( data );
  104. ConsolePrint( "\r\n" );
  105. #endif
  106.     for( i= 0 ; *data != ' ' ;){
  107.         i*= 10;
  108.         i+= *data++ & 15;
  109.     }
  110.     *size= i;
  111.     for( i= 0, data++ ; *data && *data != ' ' ;){
  112.         i<<= 3;
  113.         i+= *data++ & 7;
  114.     }
  115. #if 0
  116. {
  117.     char    buf[256];
  118.     sprintf( buf, "<sec=%d>\r\n", i );
  119.     ConsolePrint( buf );
  120. }
  121. #endif
  122.     return    i;    /* time stamp */
  123. }
  124.  
  125.